<template>
	<view class="wrapper"><dlContacts ref="jContacts" :myList="myList" @confirm="contactsConfirm"></dlContacts></view>
</template>

<script>
import dlContacts from '@/components/dl-contacts/dl-contacts.vue';
export default {
	components: {
		dlContacts
	},
	data() {
		return {
			myList: [],
			roomid: '',
			text:[]
		};
	},
	onLoad(e) {
		this.roomid = e.roomid;
		this.memberList();
	},
	methods: {
		contactsConfirm(res) {
			res.map(item => {
				this.text.push(JSON.stringify(item.toUserId));
			});
			this.$ajax(
				this.apiList.memberUpdate,
				{
					roomId: this.roomid,
					text: JSON.stringify(this.text)
				},
				'POST'
			).then(res => {
				this.$sToast('邀请成功')
				this.$back(1,1500)
			});
		},
		// 查询所有好友列表
		getFriendList(list) {
			const that = this;
			this.$ajax(this.apiList.friendsList, {
				userId: uni.getStorageSync('userinfo').userId
			}).then(res => {
				res.data = res.data.filter(item => {
					return item.toNickname != '客服公众号';
				});
				// 过滤已在群组中的
				res.data = res.data.filter(item => {
					let index = 0;
					list.map(val => {
						if (val.userId == item.toUserId) {
							index = 1;
						}
					});
					return index == 0;
				});
				this.myList = res.data;
				setTimeout(function() {
					that.$refs.jContacts.show();
				}, 300);
			});
		},
		// 查询当前群组用户列表
		memberList() {
			this.$ajax(
				this.apiList.memberList,
				{
					roomId: this.roomid
				},
				'POST'
			).then(res => {
				this.getFriendList(res.data);
			});
		}
	}
};
</script>

<style lang="scss"></style>
